<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="top">
      <el-form-item label="群组信息">
        <el-link type="success" disabled>{{ this.queryParams.title }}</el-link>
        <el-link type="primary" disabled>{{ form.chatId }}</el-link>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" placeholder="请选择状态">
          <el-option
            v-for="dict in dict.type.bot_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="执行时间" prop="cronExpression">
        <el-input v-model="form.cronExpression" placeholder="cron执行表达式">
          <template slot="append">
            <el-button type="primary" @click="handleShowCron">
              生成表达式
              <i class="el-icon-time el-icon--right"></i>
            </el-button>
          </template>
        </el-input>
        <el-collapse>
          <el-collapse-item title="常用cron表达式例子" name="1">
            <el-divider content-position="left">点击表达式可自动粘贴</el-divider>

            <div @click="copyCron('0 0/5 * * * ?','每5分钟执行')">0 0/5 * * * ?</div>
            <el-divider content-position="left">每5分钟执行</el-divider>

            <div @click="copyCron('0 0 0/1 * * ?','每1小时执行')">0 0 0/1 * * ?</div>
            <el-divider content-position="left">每1小时执行</el-divider>

            <div @click="copyCron('0 15 10 * * ?','每天上午10:15执行')">0 15 10 * * ?</div>
            <el-divider content-position="left">每天上午10:15执行</el-divider>

            <div @click="copyCron('0 0 12 * * ?','每天中午12点执行')">0 0 12 * * ?</div>
            <el-divider content-position="left">每天中午12点执行</el-divider>

            <div @click="copyCron('0 0 2 1 * ?','每月的1日的凌晨2点执行')">0 0 2 1 * ?</div>
            <el-divider content-position="left">每月的1日的凌晨2点执行</el-divider>

            <div @click="copyCron('0 0 10,14,16 * * ?','每天上午10点，下午2点，4点执行')">0 0 10,14,16 * * ?</div>
            <el-divider content-position="left">每天上午10点，下午2点，4点执行</el-divider>

            <div @click="copyCron('0 15 10 15 * ?','每月15日上午10:15执行')">0 15 10 15 * ?</div>
            <el-divider content-position="left">每月15日上午10:15执行</el-divider>

            <div @click="copyCron('0 15 10 L * ?','每月最后一日的上午10:15执行')">0 15 10 L * ?</div>
            <el-divider content-position="left">每月最后一日的上午10:15执行</el-divider>

          </el-collapse-item>
        </el-collapse>
      </el-form-item>
      <el-form-item label="任务名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入任务名称"/>
      </el-form-item>
      <el-form-item prop="config">
        <line-button ref="lineButton" label="任务内容" placeholder="请输入任务内容" :data="form.config"></line-button>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>

    <el-dialog title="Cron表达式生成器" :visible.sync="openCron" append-to-body destroy-on-close class="scrollbar">
      <crontab @hide="openCron=false" @fill="crontabFill" :expression="expression"></crontab>
    </el-dialog>
  </div>
</template>

<script>
import {getJob, addJob, updateJob} from "@/api/bot/job";
import lineButton from "@/components/bot/inline_button.vue";
import Crontab from "@/components/Crontab/index.vue";

export default {
  name: "JobEdit",
  dicts: ['bot_status'],
  components: {Crontab, lineButton},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "",
      // 是否显示Cron表达式弹出层
      openCron: false,
      // 传入的表达式
      expression: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jobId: null,
        chatId: null,
        name: null,
        status: null,
        title: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        chatId: [
          {required: true, message: "群组ID不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "任务名称不能为空", trigger: "blur"}
        ],
        config: [
          {required: true, message: "发送内容不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "状态不能为空", trigger: "change"}
        ],
        cronExpression: [
          {required: true, message: "cron表达式不能为空", trigger: ['blur', 'change']}
        ]
      }
    };
  },
  created() {
    this.queryParams = this.$route.query
    this.queryParams.title = this.$route.query.params.chatName
    this.handleUpdate()
  },
  methods: {
    //返回上个页面
    back(){
      this.$tab.closeOpenPage({ path: "/tg/job" });
    },
    // 表单重置
    reset() {
      this.form = {
        id: this.queryParams.id,
        jobId: null,
        chatId: this.queryParams.chatId,
        name: null,
        cronExpression: null,
        config: {
          chatId: this.queryParams.chatId,
          content: "",
          buttonRows: [
            []
          ],
        },
        remark: null,
        orderNum: null,
        status: '1',
        delFlag: null,
        createBy: null,
        createById: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate() {
      this.reset();
      if (this.form.id) {
        getJob(this.form.id).then(response => {
          if (response.data) {
            response.data.config = JSON.parse(response.data.config)
            //防止群变更类型时候时候配置里面的ID不变
            response.data.config.chatId = response.data.chatId
            this.form = response.data;
          }
        });
      }
    },
    /** 提交按钮 */
    submitForm() {
      let validateEmpty = this.$refs.lineButton.validateEmpty();
      if (!validateEmpty.validate) {
        this.$modal.msgWarning(validateEmpty.msg);
        return;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateJob(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.back()
          });
        }
      });
    },
    /** cron表达式按钮操作 */
    handleShowCron() {
      this.expression = this.form.cronExpression;
      this.openCron = true;
    },
    /** 确定后回传值 */
    crontabFill(value) {
      this.form.cronExpression = value;
    },
    copyCron(cron,describe){
      this.form.cronExpression = cron
      this.$modal.msgSuccess("已粘贴:"+describe);
    }
  }
};
</script>
